package switchviz.core.scenario;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ItemListener;
import java.io.IOException;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import org.jdom.JDOMException;

import switchviz.core.ResourceManager;
import switchviz.gui.component.JXMLComboBox;
import switchviz.gui.component.PaddingPanel;
import switchviz.network.netswitch.SwitchDataModel;
import switchviz.recording.RecordDataModel;

public class ScenarioSourcePanel extends PaddingPanel
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 568980179271988620L;
	private JRadioButton _radswitch = null;
	private JRadioButton _radrec = null;
	private PaddingPanel _switchselpanel = null;
	private PaddingPanel _recselpanel = null;
	private JXMLComboBox _cmbswitch = null;
	private JXMLComboBox _cmbrecord = null;
	private JPanel _selectioncontainer = null;
	
	public ScenarioSourcePanel() throws JDOMException, IOException
	{
		initialize();
	}
	
	public boolean isSwitchSelected()
	{
		return _radswitch.isSelected();
	}
	
	public RecordDataModel getRecord()
	{
		if(_cmbrecord != null)
			return (RecordDataModel)_cmbrecord.getComboBox().getSelectedItem();
	
		return null;
	}
	
	public void changeToViewMode() throws JDOMException, IOException
	{
		_selectioncontainer.removeAll();
		_selectioncontainer.add(getSwitchSelectionPanel(), BorderLayout.CENTER);
		_selectioncontainer.validate();
		_selectioncontainer.repaint();
	}
	
	public void changeToRecordMode() throws JDOMException, IOException
	{
		_selectioncontainer.removeAll();
		_selectioncontainer.add(getRecSelectionPanel(), BorderLayout.CENTER);
		_selectioncontainer.validate();
		_selectioncontainer.repaint();
	}
	
	public void reload() throws JDOMException, IOException
	{
		if(_cmbswitch != null)
			_cmbswitch.reload();
		if(_cmbrecord != null)
			_cmbrecord.reload();
	}
	
	public SwitchDataModel getSwitchDataModel()
	{
		return (SwitchDataModel)_cmbswitch.getComboBox().getSelectedItem();
	}
	
	private PaddingPanel getSwitchSelectionPanel() throws JDOMException, IOException
	{
		if(_switchselpanel == null)
		{
			_switchselpanel = new PaddingPanel();
			_switchselpanel.setLayout(new BoxLayout(_switchselpanel, BoxLayout.PAGE_AXIS));
			_cmbswitch = new JXMLComboBox("", JXMLComboBox.SWITCHLIST);
			_switchselpanel.add(_cmbswitch);
		}
		return _switchselpanel;
	}
	
	private PaddingPanel getRecSelectionPanel() throws JDOMException, IOException
	{
		if(_recselpanel == null)
		{
			_recselpanel = new PaddingPanel();
			_recselpanel.setLayout(new BoxLayout(_recselpanel, BoxLayout.PAGE_AXIS));
			
			_recselpanel.add(_cmbrecord);
		}
		return _recselpanel;
	}
	
	public void addItemListener(ItemListener l)
	{
		_radswitch.addItemListener(l);
		_cmbrecord.getComboBox().addItemListener(l);
	}
	
	private void initialize() throws JDOMException, IOException
	{
		_selectioncontainer = new JPanel(ResourceManager.getInstance().getBorderLayout());
		
		_cmbrecord = new JXMLComboBox("", JXMLComboBox.RECORDLIST);
		
		ButtonGroup buttGroup = new ButtonGroup();
		_radswitch = new JRadioButton("Switch");
		_radrec = new JRadioButton("Record");
		buttGroup.add(_radswitch);
		buttGroup.add(_radrec);
		
		_radswitch.setSelected(true);
		_selectioncontainer.add(getSwitchSelectionPanel());
		
		this.setLayout(ResourceManager.getInstance().getBorderLayout());
		this.setBorder(BorderFactory.createTitledBorder("Select Source"));
		this.setMaximumSize(new Dimension(800, 100));
		
		PaddingPanel optionpanel = new PaddingPanel();
		optionpanel.setLayout(new BoxLayout(optionpanel, BoxLayout.PAGE_AXIS));
	    optionpanel.add(_radswitch);
	    optionpanel.add(_radrec);
	    
	    this.add(optionpanel, BorderLayout.WEST);
	    this.add(_selectioncontainer, BorderLayout.CENTER);
	    this.setPreferredSize(new Dimension(400, 100));
	}

}
